<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleAdd">删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" border>
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="角色名称" align="center" prop="name" />
      <el-table-column label="状态" align="center" prop="" />
      <el-table-column label="创建时间" align="center" prop="" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" @click="handleUpdate(scope.row)" plain icon="el-icon-edit">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改角色对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="noticeTitle">
          <el-input v-model="form.noticeTitle" placeholder="请输入角色名称" style="width:380px" />
        </el-form-item>
        <el-form-item label="菜单权限" prop="noticeTitle">
          
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <el-drawer :title="title" :before-close="handleClose" :visible.sync="open" direction="rtl"
      custom-class="demo-drawer" ref="drawer">
      <div class="demo-drawer__content">
        <el-form :model="form" label-width="100px" :rules="rules">
          <el-form-item label="角色名称" prop="noticeTitle">
            <el-input v-model="form.noticeTitle" placeholder="请输入角色名称" style="width:90%" />
          </el-form-item>
          <el-form-item label="状态" v-show="status =='change'">
            <el-radio-group v-model="form.resource">
              <el-radio label="正常"></el-radio>
              <el-radio label="停用"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="菜单权限" prop="noticeTitle">
            <el-checkbox-group v-model="type">
              <el-checkbox label="展开/折叠" name="type"></el-checkbox>
              <el-checkbox label="全选/全不选" name="type"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item>
            <el-tree class="tree" :data="treeData" show-checkbox @check-change="handleCheckChange">
            </el-tree>

          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button class="drawerBtn" type="primary">确 定</el-button>
          <el-button class="drawerBtn" @click="cancelForm">取 消</el-button>
        </div>
      </div>
    </el-drawer>


  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";

export default {
  name: "Notice",
  data() {
    return {
      treeData: [
        {
          id: 1,
          label: '设备中心',
          children: [
            {
              id: 11,
              label: '运营模式',
            },
            {
              id: 12,
              label: '营业分组',
            },
            {
              id: 13,
              label: '设备管理',
            }
          ]
        },
        {
          id: 2,
          label: '用户中心',
          children: [
            {
              id: 21,
              label: '钱包管理',
            },
            {
              id: 22,
              label: '钱包退款',
            }
          ]
        },
        {
          id: 3,
          label: '交易明细',
          children: [
            {
              id: 31,
              label: '消费明细',
            },
            {
              id: 32,
              label: '充值明细',
            }
          ]
        },
        {
          id: 4,
          label: '账户中心',
          children: [
            {
              id: 41,
              label: '账号管理',
            },
            {
              id: 42,
              label: '角色管理',
            },
            {
              id: 43,
              label: '支付账户',
            }
          ]
        },
      ],
      status:"",
      type: [],
      searchType: "phone",
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      tableData: [{ name: "张三" }, { name: "李四" }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        noticeType: [
          { required: true, message: "公告类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    handleCheckChange(data, checked, indeterminate) {
      console.log(data, checked, indeterminate);
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    },
    cancelForm() {
      this.open = false;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加角色";
      this.status=""
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改角色";
      this.status="change"
      // const noticeId = row.noticeId || this.ids
      // getNotice(noticeId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改公告";
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noticeId != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>

<style>
/* .app-container{
  height: 100vh;
} */
/* .demo-drawer{
  position: absolute;
  height: 100%;
} */
.demo-drawer__footer {
  /* height: 100%; */
  text-align: center;
  margin-top: 50%;
}

.drawerBtn {
  width: 40%;
  height: 40px;
  font-size: 14px;
}

.tree {
  width: 90%;
  height: 200px;
  overflow-y: auto;
  border: 2px solid rgb(242, 238, 238);
}
</style>
